package com.hckjsec.common.holiday;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.constant.Constants;
import com.hckjsec.common.core.redis.RedisCache;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.http.HttpUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 此类为从网站中获取节假日
 */
@Component
public class HolidayUtil {
    @Autowired
    private RedisCache redisCache;
    public static String URL = "http://timor.tech/api/holiday/year/";// 拉取节假日地址
    public static String START_LAW_DATE = "08:30";//上班时间
    public static String END_LAW_DATE = "17:30";//下班时间
    /*
    *获取节假日Map
    */
    public Map<String,HolidayData> getHoliday(int year){
        Map<String,HolidayData> holidayHt = redisCache.getCacheObject(ComUtil.getHolidayCacheKey(String.valueOf(year)));
        if (StringUtils.isEmpty(holidayHt)){
            String rspStr = HttpUtils.sendGet(URL+year, "", Constants.UTF8);
            JSONObject jsonObject = new JSONObject(rspStr.toString());
            JSONObject data= jsonObject.getJSONObject("holiday");
            int code= jsonObject.getInt("code");
            Map<String,HolidayData> findHolidayHt = new HashMap<>();
            if (code == 0){//拉取成功
                Iterator<String> iterator = data.keys();
                while (iterator.hasNext()) {
                    String key = iterator.next();//获取key
                    String value = JSON.toJSONString(data.get(key));//获取value
                    HolidayData holidayData = JSON.parseObject(value,HolidayData.class);
                    findHolidayHt.put(holidayData.getDate(),holidayData);
                }
                holidayHt = findHolidayHt;
                redisCache.setCacheObject(ComUtil.getHolidayCacheKey(String.valueOf(year)),findHolidayHt);
            }
        }
        return holidayHt;
    }


    /*
     *获取是否在指定范围内
     */
    public boolean getIsHoliday(String startDate,String endDate){
        boolean msg = true;
        //获取当前年度
        String year =  DateUtils.parseDateToStr(DateUtils.YYYY,DateUtils.getNowDate());
        Map<String,HolidayData> holidayHt = getHoliday(Integer.valueOf(year));
        //当前日期
        boolean flag = true;
        String date =  DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.getNowDate());
        if (holidayHt.containsKey(date)){//日期如果包含 就以包含为主
            flag = holidayHt.get(date).isHoliday();//如果为TRUE 就说明date 是放假日，如为FALSE 是上班日期
        }else{//日期如不包含  则以正常周六周日来判断
            int week=DateUtils.getCurWeekDayByStr(date);
            if(week == 1 || week == 7){//周六周日
                flag = true;
            }else{
                flag = false;
            }
        }
        if (flag){
            msg = false;  //表示是放假日  禁止操作
        }else{ //判断是否是在上下班时间范围内
            try {
                Date nowTime = DateUtils.dateTime(DateUtils.HH_MM_SS,DateUtils.parseDateToStr(DateUtils.HH_MM_SS,DateUtils.getNowDate()));
                Date startTime = new SimpleDateFormat(DateUtils.HH_MM_SS).parse(startDate);
                Date endTime = new SimpleDateFormat(DateUtils.HH_MM_SS).parse(endDate);
                msg = DateUtils.isEffectiveDate(nowTime, startTime, endTime);
            } catch (ParseException e) {
                msg = false;
            }
        }
        return msg;
    }


    public void setTxt(String msg){
        try {
            FileOutputStream fos = new FileOutputStream("D:/home/jeethink/remind.txt",true);
            fos.write(msg.getBytes());
            fos.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

    }
}
